
<div class="content-wrap">
    <div class="panel panel-default">
        <div class="panel-body">
            <div class="form-horizontal font-12">

                    <ul class="nav nav-tabs font-12">
                        <li role="presentation" class="active"><a
                                    aria-expanded="true" href="#tabHomeEx1" data-toggle="tab">填写基本资料</a></li>
                        <!-- 												<li class="" role="presentation"><a aria-expanded="false" -->
                        <!-- 													href="#profileHomeEx1" data-toggle="tab">分配菜单</a></li> -->
                        <li class="" role="presentation"><a aria-expanded="false"
                                                            href="#repsd" data-toggle="tab">修改密码</a></li>
                    </ul>

                <div class="tab-content b-all p-20">
                    <div class="tab-pane fade active in" id="tabHomeEx1">
                        <form id="form1" class="form-horizontal bv-form"
                              novalidate="novalidate" modelAttribute="organization"
                              method="post">
                            <input type="hidden" name="id" id="id"
                                   value="${organization.user.orgId }" />

                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入用户名" class="form-control"
                                           data-bv-field="loginName" name="loginName" id="loginName"
                                           value="${organization.user.loginName }"
                                           onblur="javascript:checkLoginName()" />
                                </div>
                            </div>

                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">密码</label>

                                        <div class="col-sm-8">
                                            <input type="password" placeholder="请输入密码"
                                                   class="form-control" data-bv-field="password"
                                                   name="password" value="${organization.user.password }" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">重复密码</label>

                                        <div class="col-sm-8">
                                            <input type="password" placeholder="请输入密码"
                                                   class="form-control" data-bv-field="confirmPassword"
                                                   name="confirmPassword" />
                                        </div>
                                    </div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">机构名称</label>

                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入机构名称" class="form-control"
                                           data-bv-field="name" name="name" id="name"
                                           value="${organization.name }" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">所在地区</label>

                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入所在地区" class="form-control"
                                           data-bv-field="area" name="area" value="${organization.area}" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">联系人</label>

                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入联系人" class="form-control"
                                           data-bv-field="master" name="master"
                                           value="${organization.master }" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">联系人电话</label>

                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入联系人电话" class="form-control"
                                           data-bv-field="phone" name="phone"
                                           value="${organization.phone }" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">设备数量</label>

                                <div class="col-sm-8">
                                    <input type="text" placeholder="请输入设备数量" class="form-control"
                                           data-bv-field="deviceNumbers" name="deviceNumbers"
                                           value="${organization.deviceNumbers }" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户状态</label>

                                <div class="col-sm-8">
                                    <label class="checkbox-inline"> <input type="radio"
                                                                           data-bv-field="useable" value="1" name="useable"
                                                                           ${organization.user.useable eq 1 ? 'checked' : '' }>
                                        启用
                                    </label> <label class="checkbox-inline"> <input type="radio"
                                                                                    data-bv-field="useable" value="0" name="useable"
                                                                                    ${organization.user.useable eq 0 ? 'checked' : '' }>
                                        禁用
                                    </label>
                                </div>
                            </div>







                            <div class="form-group has-feedback ">
                                <label class="col-sm-2 control-label">服务期限</label>

                                <div class="col-sm-3">
                                    <div class='input-group  datepick datepickstart'>
                                        <input type='text' class="form-control" name="StartDate" value="<fmt:formatDate value="${organization.startDate}"/>"  placeholder="请选择开始日期"  data-bv-field="StartDate"/>
                                                <span class="input-group-addon">
                                                    <span class="glyphicon glyphicon-calendar"></span>
                                                </span>
                                    </div>
                                </div>
                                <div class="col-sm-3">
                                    <div class='input-group  datepick datepickend'>
                                        <input type='text' class="form-control" value="<fmt:formatDate value="${organization.endDate}"/>"  name="EndDate" placeholder="请选择结束日期" data-bv-field="EndDate"/>
                                                <span class="input-group-addon">
                                                    <span class="glyphicon glyphicon-calendar"></span>
                                                </span>
                                    </div>

                                </div>
                            </div>





                            <div class="form-group">
                                <div class="col-lg-9 col-lg-offset-3">
                                    <button class="btn btn-primary" type="button"
                                            id="subDefaultForm">保存</button>


                                            <button id="resetBtn" class="btn btn-info" type="reset">重置</button>

                                </div>
                            </div>

                        </form>
                    </div>








                    <div class="tab-pane fade" id="profileHomeEx1">
                        <form action="${ctx}/user/saveMenuPre" id="form2"
                              onsubmit="return checkData()">
                            <input type="hidden" name="userId" id="userId"
                                   value="${organization.user.id }"> <input type="hidden"
                                                                            name="menuPre" id="menuPre" value="" />
                            <div class="form-group">
                                <div class="col-sm-12">

                                        <div class="checkbox-inline">
                                            <div class="custom-checkbox font-12 checkbox-green">
                                                <input type="checkbox" id="chk${status.index+1 }"
                                                       name="menucheck" value="${menu.id }" ${menu.checked }>
                                                <label for="chk${status.index+1 }">${menu.menuName }</label>
                                            </div>
                                        </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-lg-9 col-lg-offset-3">
                                    <button class="btn btn-primary" type="submit">保存</button>
                                </div>
                            </div>
                        </form>
                    </div>



                    <div class="tab-pane fade" id="repsd">
                        <form novalidate="novalidate" class="form-horizontal bv-form"
                              method="post" id="form3" modelAttribute="user"
                              action="${ctx}/user/repsd">
                            <input type="hidden" name="userId" id="userId"
                                   value="${organization.user.id }">
                            <div class="tab-content b-all no-b-t p-20">
                                <div class="tab-pane fade active in">
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">新密码</label>
                                        <div class="col-sm-8">
                                            <input type="password" placeholder="请输入新密码"
                                                   class="form-control" data-bv-field="newPassword"
                                                   name="newPassword" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">重复新密码</label>
                                        <div class="col-sm-8">
                                            <input type="password" placeholder="请输入新密码"
                                                   class="form-control" data-bv-field="confirmnewPassword"
                                                   name="confirmnewPassword" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <hr class="line-dashed">

                            <div class="form-group">
                                <div class="col-lg-9 col-lg-offset-3">
                                    <button class="btn btn-primary" type="submit" id="setPwd">保存</button>
                                    <!--<button id="resetBtn" class="btn btn-info" type="button">重置</button> -->
                                </div>
                            </div>
                        </form>
                    </div>
                </div>










            </div>
            <hr class="line-dashed">

        </div>
    </div>
</div>



<%-- <link href="${ctx}/static/stylesheets/bootstrap-datetimepicker.css" rel="stylesheet"> --%>
<%-- <script src="${ctx}/static/javascripts/moment-with-locales.js"></script> --%>
<%-- <script src="${ctx}/static/javascripts/bootstrap-datetimepicker.js"></script> --%>

<script type="text/javascript">
    // 	function subForm(){
    // 		$.ajax({
    // 			type : "POST",
    // 			url : "${ctx}/user/save",
    // 			dataType : "json",
    // 			data : $('#defaultForm').serialize(),
    // 			success : function(data) {
    // 				if (data.hasOwnProperty("msg")) {
    // 					if("sucess"== data.msg){
    // 						alert("操作成功！");
    // 						window.location.href='${ctx}/user/list';
    // 					}else{
    // 						alert("网络异常！");
    // 						return false;
    // 					}
    // 				}else{
    // 					alert("网络异常！");
    // 					return false;
    // 				}
    // 			}
    // 		});
    // 	}









    //分配权限
    function checkData() {
        var chk_value = [];//定义一个数组
        $('input[name="menucheck"]').each(function() {
            if ($(this).is(':checked')) {
                chk_value.push($(this).val()); //将选中的值添加到数组chk_value
            }
        });
        if (chk_value.length > 0) {
            $("#menuPre").val(chk_value);
            return true;
        } else {
            if (confirm("您将删除此用户拥有的所有菜单权限？？？？")) {
                $("#menuPre").val("");
                return true;
            } else {
                return false;
            }
        }
    }

    function checkLoginName() {
        if ($("#id").val() > 0) {
            return false;
        }
        if ($("#loginName").val().length > 0) {
            $.ajax({
                type : "POST",
                url : "${ctx}/user/checkLoginName",
                dataType : "json",
                data : {
                    loginName : $("#loginName").val()
                },
                success : function(data) {
                    if (data.hasOwnProperty("message")) {
                        alert(data.message);
                        $("#loginName").val("");
                        $("#loginName").focus();
                    } else {
                        return true;
                    }
                }
            });
        } else {
            $("#loginName").focus();
            return false;
        }
    }


    function dateCheck(){
        var s = $('input[name=StartDate]').val();
        //alert(s);
        var ds = new Date(s);
        //alert(ds);
        var e = $('input[name=EndDate]').val();
        var de = new Date(e);
        if(Date.parse(ds) - Date.parse(de)>0){
            alert("结束日期不能早于开始日期");
            return false;
        }
        return true;
    }

    function MyValidateForm(){
        $('#form1').bootstrapValidator({
            message : 'This value is not valid',
            feedbackIcons : {
                valid : 'glyphicon glyphicon-ok',
                invalid : 'glyphicon glyphicon-remove',
                validating : 'glyphicon glyphicon-refresh'
            },
            fields : {
                loginName : {
                    validators : {
                        notEmpty : {
                            message : '用户名不能为空'
                        },
                        stringLength : {
                            max : 20,
                            message : '用户名不超过20个字符'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_]+$/,
                            message: '用户名只能包含大写、小写、数字和下划线'
                        }
                    }
                },
                password : {
                    validators : {
                        notEmpty : {
                            message : '密码不能为空'
                        },
                        regexp : {
                            regexp : /[0-9a-zA-Z]/,
                            message : '密码只能由字母、数字组成'
                        },
                        stringLength : {
                            min : 6,
                            message : '密码不能小于6为'
                        }
                    }
                },
                confirmPassword :{
                    validators : {
                        notEmpty : {
                            message : "密码不能为空"
                        },
                        identical : {
                            field : 'password',
                            message : "密码不一致"
                        }
                    }
                },
                name : {
                    validators : {
                        stringLength : {
                            max : 50,
                            message : '机构名称不能超过50个字符'
                        }
                    }
                },
                area : {
                    validators : {
                        stringLength : {
                            max : 50,
                            message : '所在地区不能超过50个字符'
                        }
                    }
                },
                master : {
                    validators : {
                        notEmpty : {
                            message : '联系人不能为空'
                        },
                        stringLength : {
                            max : 20,
                            message : '联系人不能超过20个字符'
                        }
                    }
                },
                phone : {
                    validators : {
                        notEmpty : {
                            message : '联系电话不能为空'
                        },
                        regexp : {
                            regexp : /^1[34578]\d{9}$/,
                            message : "手机号不正确"
                        }
                    }
                },
                useable : {
                    validators : {
                        notEmpty : {
                            message : '请选择状态'
                        }
                    }
                },
                StartDate : {
                    validators : {
                        notEmpty : {
                            message : '请选择开始日期'
                        }
                    }
                },
                EndDate : {
                    validators : {
                        notEmpty : {
                            message : '请选择结束日期'
                        }
                    }
                }
            }
        }).on('success.form.bv', function (e) {
            var para = $("#form1").serialize();
            $.post("${ctx}/user/save",para,function(data){
                if(data.hasOwnProperty("msg")){
                    if("success" == data.msg){
                        setTimeout(golist(), 1000);
                    }else{
                        alert(data.msg);
                        return false;
                    }
                }else{
                    alert("网络异常！");
                    return false;
                }
            },'json');
        });
    }
    $(document).ready(function() {
        function randomNumber(min, max) {
            return Math.floor(Math.random() * (max - min + 1) + min);
        };
        $('#captchaOperation').html([ randomNumber(1, 100), '+',randomNumber(1, 200), '=' ].join(' '));
        if($("#id").val() > 0){
            MyValidateForm();
        }else{
            MyValidateForm();
        }



        $('#form3').bootstrapValidator({
            message : 'This value is not valid',
            feedbackIcons : {
                valid : 'glyphicon glyphicon-ok',
                invalid : 'glyphicon glyphicon-remove',
                validating : 'glyphicon glyphicon-refresh'
            },
            fields : {
                newPassword : {
                    validators : {
                        notEmpty : {
                            message : '密码不能为空'
                        },
                        regexp : {
                            regexp : /[0-9a-zA-Z]/,
                            message : '密码只能由字母、数字组成'
                        },
                        stringLength : {
                            min : 6,
                            message : '密码不能小于6位'
                        }
                    }
                },
                confirmnewPassword :{
                    validators : {
                        notEmpty : {
                            message : "密码不能为空"
                        },
                        identical : {
                            field : 'newPassword',
                            message : "密码不一致"
                        }
                    }
                }
            }
        });






        // Validate the form manually
        $('#subDefaultForm').click(function() {
            $('#form1').bootstrapValidator('validate');
        });

        $('#resetBtn').click(function() {
            $('#form1').data('bootstrapValidator').resetForm(true);
        });


        $("#setPwd").click(function(){
            $('#form3').bootstrapValidator('validate');
        });


    });



    $(function () {

        $('.datepickstart').datetimepicker({
            locale: 'zh-cn',
            format: 'YYYY-MM-DD'
        });
        $('.datepickend').datetimepicker({
            locale: 'zh-cn',
            format: 'YYYY-MM-DD',
            useCurrent: false
        });
    })
    $('.datepick')
            .on('dp.change dp.show', function (e) {
                // Validate the date when user change it
                $('#form1').data('bootstrapValidator').validateField('StartDate');
                $('#form1').data('bootstrapValidator').validateField('EndDate');
            });
    if ($("#id").val() > 0) {
        $("#loginName").attr("readonly", true);
        $('input[name=StartDate]').attr("readonly",true);

    }else{
        if(!$("input[name=StartDate]").val() && !$("input[name=EndDate]").val())
        {
            $(".datepickstart").on("dp.change", function (e) {
                console.log("dp.change");
                $('.datepickend').data("DateTimePicker").minDate(e.date);
            });
            $(".datepickend").on("dp.change", function (e) {
                $('.datepickstart').data("DateTimePicker").maxDate(e.date);
            });
        }

    }



    function golist(){
        window.location.href = '${ctx}/user/list';
    }

</script>

